package com.project2.controller;

import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;
import com.project2.bean.bo.BrandCreateBo;
import com.project2.bean.bo.BrandDeleteBo;
import com.project2.bean.bo.BrandUpdateBo;
import com.project2.bean.vo.DataInAdminBrandVo;
import com.project2.bean.vo.DataInBrandUpdateVo;
import com.project2.bean.vo.ListInDataInAdminBrandVo;
import com.project2.service.AdminBrandService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangminghui
 * @date 2022/8/13
 */
@RestController
@RequestMapping("admin/brand")
public class AdminBrandController {
    @Autowired
    AdminBrandService adminBrandService;

    @RequiresPermissions("admin:brand:list")
    @RequestMapping("list")
    public ResultVo list(Integer id, String name, PageBo pageBo) {
        CommonData<ListInDataInAdminBrandVo> data = adminBrandService.queryAllBrandInfo(id, name, pageBo);
        return ResultVo.ok(data);
    }

    @RequiresPermissions("admin:brand:create")
    @RequestMapping("create")
    public ResultVo list(@RequestBody BrandCreateBo brandCreateBo) {
        adminBrandService.addBrandInfo(brandCreateBo);
        // queryNewAddBrandInfoByNameAndDesc
        DataInAdminBrandVo data = adminBrandService.queryNewAddBrandInfoByNameAndDesc(
                brandCreateBo.getName(), brandCreateBo.getDesc());
        return ResultVo.ok(data);
    }

    @RequiresPermissions("admin:brand:update")
    @RequestMapping("update")
    public ResultVo update(@RequestBody BrandUpdateBo brandUpdateBo) {
        int count = adminBrandService.updateBrandInfo(brandUpdateBo);
        DataInBrandUpdateVo data = adminBrandService.queryUpdateBrandInfoById(brandUpdateBo.getId());
        return ResultVo.ok(data);
    }

    @RequiresPermissions("admin:brand:delete")
    @RequestMapping("delete")
    public ResultVo delete(@RequestBody BrandDeleteBo brandDeleteBo) {
        adminBrandService.deleteBrandInfoById(brandDeleteBo.getId());
        return ResultVo.ok();
    }

}
